package org.esky.spring.ioc;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;

/**
 * 读取配置文件实例化一个IoC容器
 * @return ApplicationContext
 * @dateTime 2012-5-29上午9:38:32
 * @author Jack.xu
 */
@SuppressWarnings("deprecation")
public class ApplicationContextFactory {
	private static final Log LOG = LogFactory.getLog(ApplicationContextFactory.class);
	
	private static ApplicationContext context = null;
	private static org.springframework.beans.factory.BeanFactory  beanFactory = null;
	
	// TERM 1:ClassPathXmlApplicationContext
	public static ApplicationContext getContextByClassPathXml(String[] configLocations) {
		context = new ClassPathXmlApplicationContext(configLocations);
		LOG.info("ClassPathXmlApplicationContext \t" + context.getClass());
		return context;
	}
	
	// TERM 2:XmlBeanFactory
	public static org.springframework.beans.factory.BeanFactory getContextByXmlBeanFactory(Resource resource) {
		beanFactory = new XmlBeanFactory(resource);
		LOG.info("XmlBeanFactory \t" + beanFactory.getClass());
		return beanFactory;
	}
	
	// TERM 3:FileSystemXmlApplicationContext
	public static BeanFactory getContextByFileSystemXmlApplicationContext(String[] configLocations) {
		beanFactory = new FileSystemXmlApplicationContext(configLocations);
		LOG.info("FileSystemXmlApplicationContext \t" + beanFactory.getClass());
		return beanFactory;
	}
	
	// TERM 4:WebApplicationContextUtils
//	public static ApplicationContext getContextByWebApplicationContextUtils() {
//		ServletContext servletContext = request.getSession().getServletContext();    
//	    context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//		LOG.info("WebApplicationContextUtils \t" + context.getClass());
//		return context;
//	}
	
}
